if ("`swdLocal'\Data"=="") {
	di as error "Please run init.do first."
	error 1
}
clear all
set more off
set maxvar 10000

*Open file for regressions
use "${gsdAnalysisOutput}/bscie_panel_indices_final", clear

*Keep only endline and lagged controls
keep if wave==2 & final_sample==1
drop if replacement == 1 & gender == 1

*Define strata
egen strata=group(gender state_old)

gen treatment_fem = treatment*gender
gen lg_kcb_dist_fem = lg_kcb_dist*gender

*Generate treatment streams
gen treatmentX=0
replace treatmentX=1 if treatment_stream==3
gen treatmentX_fem = treatmentX*gender

ren edu_level_b  edu_b

*Generate IV treatment x kcb_dist 
gen Z_treat_kcb=treatment*lg_kcb_dist
label var Z_treat_kcb "Treatment x (log) distance to KCB branch"
gen Z_treat_kcb_fem = Z_treat_kcb*gender

*Generate interaction for other geographic controls
gen treat_citydist=treatment*lg_city_dist
label var treat_citydist "Treatment x (log) distance to city center"
gen treat_roaddist=treatment*lg_road_dist
label var treat_roaddist "Treatment x (log) distance to primary road"
gen treat_gradient=treatment*gradient
label var treat_gradient "Treatment x gradient"
gen treat_conflict = treatment*death_x_prox_a300
label var treat_conflict "Treatment x conflict_affected_index(300km buffer)"
gen treat_edu = treatment*edu_b
gen treat_num = treatment*num_eval_b
gen treat_lit = treatment*lit_eval_b


* Generate sum of interaction and level
gen TplusD = treatment + lg_kcb_dist 
gen TtimesDminus1 = treatment * (lg_kcb_dist - 1)
gen TplusNogrant = treatment + treatmentX
gen TtimesNOgrantminus1 = treatment * (treatmentX - 1)

*Define female controls
gen lg_city_dist_fem =  lg_city_dist*gender
gen treat_citydist_fem = treat_citydist*gender
gen lg_road_dist_fem = lg_road_dist*gender
gen treat_roaddist_fem = treat_roaddist*gender
gen gradient_fem = gradient*gender
gen treat_gradient_fem = treat_gradient*gender
gen death_x_prox_a300_fem = death_x_prox_a300*gender
gen treat_conflict_fem = treat_conflict*gender

gen edu_b_fem = edu_b*gender
gen treat_edu_fem = treat_edu*gender
gen num_eval_b_fem = num_eval_b*gender
gen treat_num_fem = treat_num*gender
gen lit_eval_b_fem = lit_eval_b*gender
gen treat_lit_fem = treat_lit*gender

*Define geographic controls
global geo_controls lg_city_dist treat_citydist treat_roaddist lg_road_dist gradient /// 
treat_gradient death_x_prox_a300 treat_conflict  lg_city_dist_fem treat_citydist_fem /// 
lg_road_dist_fem treat_roaddist_fem gradient_fem treat_gradient_fem death_x_prox_a300_fem treat_conflict_fem
global base_controls i.edu_b i.treat_edu i.num_eval_b i.treat_num i.lit_eval_b i.treat_lit i.edu_b_fem i.treat_edu_fem i.num_eval_b_fem i.treat_num_fem i.lit_eval_b_fem i.treat_lit_fem


	eststo: xi: ivreg2 consumption_index lg_kcb_dist lg_kcb_dist_fem treatment treatment_fem $geo_controls i.strata ( treatmentX treatmentX_fem= Z_treat_kcb Z_treat_kcb_fem) , cluster(boma_old)  first 
		lincom treatment_fem + treatmentX_fem
	eststo: xi: ivreg2 consumption_index lg_kcb_dist lg_kcb_dist_fem treatment treatment_fem $geo_controls $base_controls i.strata ( treatmentX treatmentX_fem= Z_treat_kcb Z_treat_kcb_fem) , cluster(boma_old)  first 
		lincom treatment_fem + treatmentX_fem

		
	eststo: xi: ivreg2 trust_index lg_kcb_dist lg_kcb_dist_fem treatment treatment_fem $geo_controls i.strata ( treatmentX treatmentX_fem= Z_treat_kcb Z_treat_kcb_fem) , cluster(boma_old)  first 
		lincom treatment_fem + treatmentX_fem
	eststo: xi: ivreg2 trust_index lg_kcb_dist lg_kcb_dist_fem treatment treatment_fem $geo_controls $base_controls i.strata ( treatmentX treatmentX_fem= Z_treat_kcb Z_treat_kcb_fem) , cluster(boma_old)  first 
		lincom treatment_fem + treatmentX_fem 